<template>
  <div class="white-box">
    <div class="table-mb" v-if="new_house_id">
      <el-button type="primary" @click="handleAdd({new_house_id:new_house_id})">添加楼栋</el-button>
    </div>
    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">楼栋名称：</span>
        <el-input placeholder="请输入楼栋名称" v-model="searchParams.title" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">出售状态：</span>
        <el-select v-model="searchParams.sell_status" placeholder="请选择出售状态" clearable>
          <el-option label="待售" :value="1"/>
          <el-option label="在售" :value="2"/>
          <el-option label="售馨" :value="3"/>
        </el-select>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">上架状态：</span>
        <el-select v-model="searchParams.shangjia_status" placeholder="请选择上架状态" clearable>
          <el-option label="上架" :value="1"/>
          <el-option label="下架" :value="2"/>
        </el-select>
      </div>
      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="pan_title" label="所属楼盘"> </el-table-column>
      <el-table-column prop="title" label="楼栋名称"></el-table-column>
      <el-table-column prop="zhuangxiu_label" label="装修类型"></el-table-column>
      <el-table-column prop="danyuan_num" label="基础信息">
        <template #default="{ row }">
          <el-descriptions :column="2">
            <el-descriptions-item label="单元数">{{ row.danyuan_num }}</el-descriptions-item>
            <el-descriptions-item label="总层数">{{ row.total_floor_num }}</el-descriptions-item>
            <el-descriptions-item label="总户数">{{ row.total_hu_num }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="open_time_label" label="时间">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="开盘时间">{{ row.open_time_label }}</el-descriptions-item>
            <el-descriptions-item label="交房时间">{{ row.give_time_label }}</el-descriptions-item>
<!--            <el-descriptions-item label="创建时间">{{ row.created_at }}</el-descriptions-item>-->
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="addr" label="出售">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="出售状态">
              <el-text v-if="row.sell_status==1" type="warning">{{ row.sell_status_label }}</el-text>
              <el-text v-if="row.sell_status==2" type="success">{{ row.sell_status_label }}</el-text>
              <el-text v-if="row.sell_status==3" type="danger">{{ row.sell_status_label }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item label="剩余套数">{{ row.huave_tao_num }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="shangjia_status" label="上架状态" width="150">
        <template #default="{ row }">
          <el-radio-group v-model="row.shangjia_status" size="small" @change="shangjia_status_change(row,$event)">
            <el-radio-button :label="1">上架</el-radio-button>
            <el-radio-button :label="2">下架</el-radio-button>
          </el-radio-group>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit2(row,{new_house_dong_id:row.new_house_dong_id,new_house_id:row.new_house_id})">编辑</el-button>
          <el-button link type="primary" @click="handleHu(row)">户型列表</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">

        <el-form-item label="楼栋名称" prop="title">
          <el-input v-model.trim="dialogForm.title" clearable placeholder="请输入楼栋名称"/>
        </el-form-item>
        <el-form-item label="单元个数" prop="danyuan_num">
          <el-input v-model.trim="dialogForm.danyuan_num" clearable placeholder="请输入单元个数" oninput="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
        <el-form-item label="总层数" prop="total_floor_num">
          <el-input v-model.trim="dialogForm.total_floor_num" clearable placeholder="请输入总层数" oninput="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
        <el-form-item label="总户数" prop="total_hu_num">
          <el-input v-model.trim="dialogForm.total_hu_num" clearable placeholder="请输入总户数" oninput="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
        <el-form-item label="剩余套数" prop="huave_tao_num">
          <el-input v-model.trim="dialogForm.huave_tao_num" clearable placeholder="请输入剩余套数" oninput="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
        <el-form-item label="房源装修" prop="zhuangxiu_id">
          <el-select v-model="dialogForm.zhuangxiu_id" placeholder="请选择房源装修">
            <el-option :label="item.label" :value="item.value" v-for="(item, index) in selectOptions.zhuangxiu_id"
                       :key="index"/>
          </el-select>
        </el-form-item>
        <el-form-item label="开盘时间" prop="open_time">
          <el-date-picker
              v-model="dialogForm.open_time"
              type="date"
              placeholder="选择开盘时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="交房时间" prop="give_time">
          <el-date-picker
              v-model="dialogForm.give_time"
              type="date"
              placeholder="选择交房时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="出售状态" prop="sell_status">
          <el-radio-group v-model="dialogForm.sell_status">
            <el-radio :label="1">待售</el-radio>
            <el-radio :label="2">在售</el-radio>
            <el-radio :label="3">售馨</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上架状态" prop="shangjia_status">
          <el-radio-group v-model="dialogForm.shangjia_status">
            <el-radio :label="1">上架</el-radio>
            <el-radio :label="2">下架</el-radio>
          </el-radio-group>
        </el-form-item>



        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRoute,
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import {validNumOrLetter} from "@common/utils";

const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()
const router = useRouter()
const route = useRoute()

const new_house_id = route.query.new_house_id || ''
searchParams.new_house_id = new_house_id

dialogForm.title = '';
dialogForm.sell_status = 1;
dialogForm.shangjia_status = 1;
dialogForm.zhuangxiu_id = '';
dialogForm.open_time = '';
dialogForm.give_time = '';
dialogForm.danyuan_num = '';
dialogForm.total_floor_num = '';
dialogForm.total_hu_num = '';
dialogForm.huave_tao_num = '';

apiPath.value = 'admin/house/new/dong/index';
dialogParams.addApiPath = 'admin/house/new/dong/create';
dialogParams.editApiPath = 'admin/house/new/dong/update';

onMounted(() => {
  getData()
  getSelectOptions()
})

const dialogFormRules = reactive({
  title:[{
    required: true,
    message: "请输入楼栋名称",
    trigger: "blur"
  }],
  zhuangxiu_id: [{
    required: true,
    message: "请选择房源装修",
    trigger: "change"
  }],
  sell_status: [{
    required: true,
    message: "请选择出售状态",
    trigger: "change"
  }],
  shangjia_status: [{
    required: true,
    message: "请选择上架状态",
    trigger: "change"
  }],
  open_time: [{
    required: true,
    message: "请选择开盘时间",
    trigger: "change"
  }],
  give_time: [{
    required: true,
    message: "请选择交房时间",
    trigger: "change"
  }],
  danyuan_num:[{
    required: true,
    message: "请输入单元个数",
    trigger: "blur"
  }],
  total_floor_num:[{
    required: true,
    message: "请输入总层数",
    trigger: "blur"
  }],
  total_hu_num:[{
    required: true,
    message: "请输入总户数",
    trigger: "blur"
  }],
  huave_tao_num:[{
    required: true,
    message: "请输入剩余套数",
    trigger: "blur"
  }]
})
const handleEdit2 = (row,data) => {
  dialogShow.value = true
  dialogParams.type = 'edit'
  dialogParams.data = data
  setTimeout(() => {
    for (let key in dialogForm) {
      dialogForm[key] = row[key]
    }
    dialogForm.open_time = row.open_time_label
    dialogForm.give_time = row.give_time_label
  }, 50)
}
const shangjia_status_change = (row, e) => {
  handleAction('admin/house/new/dong/changeShangJiaStatus', {
    new_house_dong_id: row.new_house_dong_id,
    shangjia_status: e
  })
}

const handleDelete=(row)=> {
  handleAction('admin/house/new/dong/delete', {
    new_house_dong_id: row.new_house_dong_id,
  })
}
const handleHu = (row) => {
  router.push({
    path: "/houseNewHu/houseNewHu",
    query: {
      new_house_dong_id: row.new_house_dong_id,
      metaTitle:`户型列表【${row.pan_title}-${row.title}】`
    }
  })
}
const selectOptionsConfig = {
  zhuangxiu_id:{url:'common/dataSelect/houseTagSelect',params:{p_id:4}}
}
const selectOptions = reactive({
  // new_house_id:[],
  zhuangxiu_id:[]
})
const getSelectOptions = () => {
  for (let key in selectOptionsConfig){
    let {url,params} = selectOptionsConfig[key]
    proxy.$axios
        .get(url,{params:params})
        .then((res) => {
          if (res.data.code == 200) {
            let arr;
            if(url == 'common/dataSelect/houseTagSelect'){
              arr = res.data.data.map(item => {
                return {
                  label:item.house_tag_title,
                  value:item.house_tag_id,
                }
              })
            }
            selectOptions[key] = arr
          } else {
            proxy.$message({
              message: res.data.msg,
              type: "error"
            })
          }
        })
        .catch((err) => {
        })
  }
}


</script>

<style lang="less">
.el-descriptions__cell {
  padding-bottom: 0 !important;
}

.el-descriptions__label {
  color: #999999 !important
}
</style>